<template>
	<view class="main">
		<u-form labelPosition="left" :model="model" ref="uForm" labelWidth='90'>
			<u-form-item label="线缆类型 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.cableClass" placeholder="请输入线缆类型">
					</u-input>
				</view>
			</u-form-item>
			<u-form-item label="规格型号 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.cableModel" placeholder="请输入规格型号"></u-input>
				</view>
			</u-form-item>

			<u-form-item label="长度单位 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.lengthUnit" placeholder="请输入长度单位">
					</u-input>
				</view>
			</u-form-item>
			<u-form-item label="重量单位 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.weightUnit" placeholder="请输入重量单位"></u-input>
				</view>
			</u-form-item>

			<u-form-item label="生产日期 :" @click="selTime">
				<u--input v-model="time2" disabled disabledColor="#ffffff" placeholder="请选择生产日期"
					border="none"></u--input>
				<u-icon slot="right" name="arrow-right"></u-icon>
			</u-form-item>
			<u-form-item label="生产厂家 :">
				<view class="" style="border: 1px solid #bcbcbc;border-radius: 16rpx;width: 100%;">
					<u-input v-model="model.producer" placeholder="请输入生产厂家"></u-input>
				</view>
			</u-form-item>
		</u-form>
		<view class="" style="margin: 30rpx 0rpx;">
			<u-button type="success" text="确定" @click="add"></u-button>
		</view>
		<view class="">
			<u-button text="取消" @click="back"></u-button>
		</view>
		<u-datetime-picker :show="show" v-model="time" mode="datetime" @cancel="show = false"
			@confirm="confirmTime"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		formatTime
	} from "../../utils/index.js"
	import {
		addCable,
		getDataDetail,
		editCable
	} from "@/common/api.js"
	export default {
		data() {
			return {
				model: {
					cableClass: "",
					cableModel: "",
					lengthUnit: "",
					weightUnit: "",
					producer: "",
				},
				time: '',
				time2: '',
				show: false,
				id: ''
			}
		},
		methods: {

			selTime() {
				this.show = true
			},

			confirmTime(e) {
				this.time2 = formatTime(e.value)
				this.show = false
			},
			back() {
				uni.navigateBack();
			},
			add() {
				let param = {
					...this.model,
					prodDate: this.time2
				}
				if (this.id) {
					param['id'] = this.id
					editCable(param).then((res) => {
						if (res.success) {
							uni.$u.toast('修改成功')
							uni.navigateBack();
						}
					})
				} else {
					addCable(param).then((res) => {
						if (res.success) {
							uni.$u.toast('添加成功')
							uni.navigateBack();
						}
					})
				}

			},
			getDetail(id) {
				getDataDetail({
					id
				}).then((res) => {
					if (res.success) {
						for (let key in this.model) {
							this.model[key] = res.data[key];
							this.time2 = res.data.prodDate;
						}
					}
				});
			}

		},
		onLoad(e) {
			if (e.id) { //编辑
				this.id = e.id
				this.getDetail(e.id)
			}
		}

	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 20rpx 30rpx;
	}
</style>